<template>
  <el-button v-if="props.isButton === true" type="primary" @click="show">{{ props.title }}</el-button>
  <el-input v-if="props.isButton === false" :style="{width: '200px'}" readonly v-model="pd.inputText" :placeholder="props.title">
    <template #prepend>
      <el-button icon="Search" size="small" @click="show"></el-button>
    </template>
  </el-input>

  <!-- 字典搜索对话框 -->
  <el-dialog :title="props.title" v-model="pd.open" width="900px" append-to-body>
    <el-form :model="pd.queryParams" ref="queryRef" :inline="true">
      <el-form-item label="数据查询" prop="dictLabel">
        <el-input v-model="pd.queryParams.name" placeholder="请输入名称" clearable style="width: 200px"
                  @keyup.enter="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row>
      <el-table v-loading="pd.loading" highlight-current-row @select="select1" @row-click="clickRow" ref="refTable"
                :data="pd.classificationList" @selection-change="handleSelectionChange" height="260px">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="编码" align="center" prop="code"/>
        <el-table-column label="名称" align="center" prop="name"/>
        <el-table-column label="财务类别" align="center" prop="accountType"/>
        <el-table-column label="内部分类" align="center" prop="internalGroup"/>
        <el-table-column label="租户" align="center" prop="tenantId"/>
      </el-table>
      <pagination v-show="pd.total > 0" :total="pd.total" v-model:page="pd.queryParams.pageNum"
                  v-model:limit="pd.queryParams.pageSize" @pagination="getList"/>
    </el-row>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleSelectd">确 定</el-button>
        <el-button @click="pd.open = false">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup name="ClassificationSearch">
import { listClassification } from "@/api/fms/classification";
import {getCurrentInstance, reactive} from "vue";
const {proxy} = getCurrentInstance();

const props = defineProps({
  // 字典名称
  title: {
    type: String,
    default: "财务记账科目"
  },
  // 是否单选
  isSingle: {
    type: Boolean,
    default: true
  },
  isButton: {
    type: Boolean,
    default: false
  },
});

let pd = reactive({
  classificationList: [],
  open: false,
  loading: true,
  total: 0,
  inputText: "",
  reParams: [],
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
  }
})

// 显示弹框
function show() {
  getList();
  pd.open = true;
}

function select1(selection,row){
  if (props.isSingle) {
    proxy.$refs["refTable"].clearSelection();
  }
  proxy.$refs["refTable"].toggleRowSelection(row,true);
}

// 查询表数据
function getList() {
  pd.loading = true;
  listClassification(pd.queryParams).then(res => {
    pd.classificationList = res.data;
    pd.total = res.total;
    pd.loading = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  pd.queryParams.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  pd.queryParams.name = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 选择操作 */
function handleSelectd() {
  pd.reParams = proxy.$refs["refTable"].getSelectionRows();
  if (pd.reParams.length == 0) {
    proxy.$modal.msgError("请选择数据，至少一行!!");
    return;
  }
  // 测试一下
  let tempdictValue = pd.reParams.map(item => item.name);
  console.log(tempdictValue);
  // inputText = reParams.map(item => item.name);
  pd.reParams.forEach(function (item) {
    console.log(item);
    pd.inputText = item.name;
  });
  pd.open = false;
  emit("selectd", pd.reParams);
}

const emit = defineEmits(["selectd"]);

defineExpose({
  show,
});

</script>